package com.ideafactory.client.print;

import android.util.Log;

import java.util.Locale;

public class StringUtils {

	public static String toStringHex(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(
						s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			s = new String(baKeyword, "utf-8");// UTF-16le:Not
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}

	public static String hexStr2Str(String hexStr) {
		hexStr = hexStr.toString().trim().replace(" ", "")
				.toUpperCase(Locale.US);
		char[] hexs = hexStr.toCharArray();
		byte[] bytes = new byte[hexStr.length() / 2];
		int iTmp = 0x00;
		;

		for (int i = 0; i < bytes.length; i++) {
			iTmp = mHexStr.indexOf(hexs[2 * i]) << 4;
			iTmp |= mHexStr.indexOf(hexs[2 * i + 1]);
			bytes[i] = (byte) (iTmp & 0xFF);
		}
		return new String(bytes);
	}

	public static String byte2HexStrBak(byte[] src, int leng){
		    StringBuilder stringBuilder = new StringBuilder("");
		    if (src == null || src.length <= 0) {   
		        return null;   
		    }   
		    for (int i = 0; i < leng; i++) {   
		        int v = src[i] & 0xFF;   
		        String hv = Integer.toHexString(v);
		       if (hv.length() < 2) {   
		            stringBuilder.append(0);   
		        }   
		        stringBuilder.append(hv);   
		    }   
		    return stringBuilder.toString();   
		}

	public static void printHexString( byte[] b, int iLen) {     
	   StringBuffer str = new StringBuffer("");
		for (int i = 0; i < b.length; i++) {    
	     String hex = Integer.toHexString(b[i] & 0xFF);
	     if (hex.length() == 1) {
	       hex = '0' + hex;    
	     }
	     str.append(hex);
	   }
	   Log.i("打印", str.toString().toUpperCase());
	}  

	public static String byte2HexStr(byte[] b, int iLen) {
		printHexString(b, iLen);
		StringBuilder sb = new StringBuilder();
		for (int n = 0; n < iLen; n++) {
			sb.append(mChars[(b[n] & 0xFF) >> 4]);
			sb.append(mChars[b[n] & 0x0F]);
			sb.append(' ');
		}
		return sb.toString().trim().toUpperCase(Locale.US);
	}

	private final static String mHexStr = "0123456789ABCDEF";

	private final static char[] mChars = "0123456789ABCDEF".toCharArray();

	public static boolean isCN(String data) {
		boolean flag = false;
		String regex = "^[\u4e00-\u9fa5]*$";
//		String regex = "^[һ-��]*$";
		if (data.matches(regex)) {
			flag = true;
		}
		return flag;
	}
}
